﻿
Imports std
Imports storoomc
Imports managedServer
Imports idsysctx
Imports occommon
Imports occommon.storoomkey

Public Module onlinecookie2
    Public Const defaultConfigfile As String = "onlinecookie.ini"

    Sub New()
        assert(strsame(occommon.storoomkey.pathSeperator, storoomc.directorystore.pathSeperator))
    End Sub

    Private Function _userSiteCookies(ByVal user As String, ByVal site As String, ByRef cookies As String) As Boolean
        Dim buff As bytes = Nothing
        If directorystore.readfile(userCookiesDirectory(user), site, buff) Then
            cookies = encoding.utf8().GetString(+buff)
            Return True
        Else
            Return False
        End If
    End Function

    Public Function userSiteCookies(ByVal user As String, ByVal site As String, ByRef cookies As String) As Boolean
        assert(Not isSharedCookieSite(site))
        Return _userSiteCookies(user, site, cookies)
    End Function

    Public Function userSharedCookies(ByVal user As String, ByRef cookies As String) As Boolean
        Return _userSiteCookies(user, sharedCookieSite, cookies)
    End Function

    Private Function _writeUserSiteCookies(ByVal user As String, ByVal site As String, ByVal cookies As String) As Boolean
        Dim buff As bytes = Nothing
        buff = New bytes(encoding.utf8().GetBytes(cookies))
        Return directorystore.writefile(userCookiesDirectory(user), site, buff)
    End Function

    Public Function writeUserSiteCookies(ByVal user As String, ByVal site As String, ByVal cookies As String) As Boolean
        assert(Not isSharedCookieSite(site))
        Return _writeUserSiteCookies(user, site, cookies)
    End Function

    Public Function writeUserSharedCookies(ByVal user As String, ByVal cookies As String) As Boolean
        Return _writeUserSiteCookies(user, sharedCookieSite, cookies)
    End Function

    Public Function userSites(ByVal user As String, ByRef sites As vector(Of String)) As Boolean
        If sites Is Nothing Then
            sites = New vector(Of String)()
        Else
            sites.clear()
        End If

        Dim v As vector(Of String) = Nothing
        If directorystore.listfiles(userCookiesDirectory(user), v) Then
            For i As Int64 = 0 To v.size() - 1
                If Not strsame(v(i), sharedCookieSite) Then
                    sites.push_back(v(i))
                End If
            Next
            Return True
        Else
            Return False
        End If
    End Function

    Public Function createIdContext(ByVal id As String,
                                    ByVal usrk As String,
                                    ByVal context As httpContext,
                                    ByVal autologin As Boolean) As idcontext
        Return New idcontext(configDelegate.idsysUrl(), id, usrk, context, autologin)
    End Function

    Public Sub main(ByVal args() As String)
        debugpause()
        ProHttpS.insertRequestProcessor(New configDelegate())
        ProHttpS.insertRequestProcessor(New siteGet())
        ProHttpS.insertRequestProcessor(New siteSet())
        ProHttpS.insertRequestProcessor(New userGet())
        ProHttpS.insertRequestProcessor(New userSetUI())
        ProHttpS.insertRequestProcessor(New userSet())
        ProHttpS.insertRequestProcessor(New userGetUI())
        ProHttpS.defaultConfigfile = defaultConfigfile
        ProHttpS.startServer(args, configNaming.onlinecookie.section)
    End Sub
End Module
